package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.entity.*;
import com.zzyl.exception.BusinessException;
import com.zzyl.mapper.*;
import com.zzyl.service.RoomService;
import com.zzyl.vo.BedVo;
import com.zzyl.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private RoomTypeMapper roomTypeMapper;
    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private CheckInConfigMapper checkInConfigMapper;
    @Autowired
    private ElderMapper elderMapper;
    //添加房间
    @Override
    public void saveOrUpdateRoom(Room room) {
        //1.根据TypeName去查询TypeId
        LambdaQueryWrapper<RoomType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoomType::getName,room.getTypeName());
        RoomType roomType = roomTypeMapper.selectOne(wrapper);
        if (roomType == null){
            throw new BusinessException("房间类型不存在");
        }
        room.setTypeId(roomType.getId());

        //2.保存或修改
        if(room.getId()== null){

            getBaseMapper().insert(room);
        }else {
            getBaseMapper().updateById(room);
        }
    }
    //查询指定的各楼层下的房间
    @Override
    public List<RoomVo> findByFloorId(Long floorId) {
        return roomMapper.findByFloorId(floorId);
    }

    //查询房间详情
    @Override
    public RoomVo findRoomById(Long id) {
        //1. 直接查询房间表  房间id 房间code 房间sort 房间typeId 房间typeName
        Room room = roomMapper.selectById(id);
        if (room == null){
            throw new BusinessException("房间不存在");
        }
        RoomVo roomVo = BeanUtil.copyProperties(room, RoomVo.class);

        //2. 根据room_id从base_bed表查询当前房间的床位信息
        LambdaQueryWrapper<Bed> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Bed::getRoomId,id);
        List<Bed> list = bedMapper.selectList(wrapper);
        //总床位数
        int totalBeds =list.size();
        //已入住床位数
        int occupiedBeds = (int)list.stream().filter(e->e.getBedStatus() == 1).count();
        //入住率
        if (totalBeds != 0 && occupiedBeds != 0){
            roomVo.setOccupancyRate(occupiedBeds*1.0/totalBeds);
        }else {
            roomVo.setOccupancyRate(0.0);
        }
        roomVo.setTotalBeds(totalBeds);
        roomVo.setOccupiedBeds(occupiedBeds);

        //4. 搞定床位集合
        List<BedVo> collect = list.stream().map(e -> {
            //创建Vo
            BedVo bedVo = new BedVo();
            //床位编号
            bedVo.setBedNumber(e.getBedNumber());

            //服务等级名称
            //根据床位号从入住配置表中查询 服务等级名称
            LambdaQueryWrapper<CheckInConfig> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(CheckInConfig::getBedNumber, e.getBedNumber());
            CheckInConfig checkInConfig = checkInConfigMapper.selectOne(wrapper1);
            if (checkInConfig != null) {
                bedVo.setLname(checkInConfig.getNursingLevelName());
                bedVo.getCheckInConfigVo().setCheckInStartTime(checkInConfig.getCheckInStartTime());
                bedVo.getCheckInConfigVo().setCheckInEndTime(checkInConfig.getCheckInEndTime());
            }


            //根据床位id查询老人信息
            LambdaQueryWrapper<Elder> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(Elder::getBedId, e.getId());
            Elder elder = elderMapper.selectOne(wrapper2);
            if (elder != null) {

                bedVo.setName(elder.getName());
            }
            return bedVo;
        }).collect(Collectors.toList());
        roomVo.setBedVoList(collect);
        return roomVo;
    }
}
